<?php
include_once('../includes/definer.php');
include_once(INC.'php/bootstrap.php');
$orderColumn = array(
    'c.value',
    'c.id'
);
$desde = $_GET['length'] * $_GET['start'];

$recordsTotal = Doctrine_Query::create()
        ->select('count(c.id)')
        ->from('Categoria c')
        ->whereNotIn('c.slug', array('prensa', 'about-me', 'home'))
        ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR)
;
//<a href=\"producto/", c.slug, "\" class=\"btn btn-primary\"><i class=\"fa fa-pencil\"></i></a>
$data = Doctrine_Query::create()
        ->select('
            c.id,
            CONCAT("row", c.id) as DT_RowId,
            CONCAT("
                <a href=\"#\" data-id=\"",c.id,"\" class=\"btn btn-danger borrarCategoria\"><i class=\"fa fa-trash-o\"></i></a>
                <a href=\"categoria/",c.slug,"\" class=\"btn btn-primary\"><i class=\"fa fa-pencil\"></i></a>
            ") as acciones,
            CONCAT("<a href=\"#\" class=\"valueEditable\" data-value=\"",c.value,"\" data-categoria=\"",c.id,"\" data-type=\"text\">",c.value,"</a>") as value
        ')
        ->from('Categoria c')
        ->whereNotIn('c.slug', array('prensa', 'about-me', 'home'))
        ->limit($_GET['length'])
        ->offset($_GET['start'])
        ->orderBy($orderColumn[$_GET['order'][0]['column']].' '.$_GET['order'][0]['dir'])
;

//busqueda
if ($_GET['search']['value']) {
    $data->andWhere('c.value like "'.$_GET['search']['value'].'%"');
}
//fin busqueda
$q2 = $data->copy();
$q2->removeDqlQueryPart('select')
        ->removeDqlQueryPart('limit')
        ->removeDqlQueryPart('offset')
        ->groupBy('1=1')
        ->addSelect('count(c.id)')
;
$recordsFiltered = $q2->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);

//echo(json_encode($productos)); exit();
$restul = array(
    'recordsTotal'=>$recordsTotal,
    'recordsFiltered'=>$recordsFiltered,
    'data'=>$data->execute(array(), Doctrine::HYDRATE_ARRAY)
);
echo(json_encode($restul));
?>